<?php

define ( 'BASE_ROOT_PATH', dirname ( dirname ( __FILE__ ) ) );
include_once (BASE_ROOT_PATH . "/word/db/DBMySQL.php");

$memberId = $_REQUEST ['id'];
if (empty ( $memberId )) {
	$msg = json_encode ( "用户id不能为空" );
	echo '{"code":"1","msg":' . $msg . '}';
	exit ();
}

$pageNo = $_REQUEST ['pageNo'];
if (empty ( $pageNo )) {
	$pageNo = 1;
}

$pageSize = $_REQUEST ['pageSize'];
if (empty ( $pageSize )) {
	$pageSize = 10;
}

$start = $pageSize * ($pageNo - 1);
// 查找用户
$db = new DBMySQL ();

$rs = $db->Query ( "select mi.member_id,mi.latitude,mi.longitude,mi.exam from t_member m join t_member_info mi on mi.member_id = m.member_id where m.member_id = $memberId limit 1" );
if (empty ( $rs )) {
	$msg = json_encode ( "该用户不存在" );
	echo '{"code":"1","msg":' . $msg . '}';
	exit ();
}
$val = $rs [0];
$longitude = $val ['longitude'];
$latitude = $val ['latitude'];
$exam = $val ['exam'];

$rs = $db->Query ( "select school_id from t_school where school_id=$schoolId limit 1" );
if (empty ( $rs )) {
	$msg = json_encode ( "该学校不存在" );
	echo '{"code":"1","msg":' . $msg . '}';
	exit ();
}

// 找该用户附近的用户,按距离从近到远排序
$rs = $db->Query ( "select count(mi.member_id) as total from t_member_info mi where mi.member_id != $memberId and mi.latitude <> '' and mi.longitude <> '' " );
$total = $rs [0] ['total'];
$dataArray = array ();
if (0 < $total) {
	if ($exam == - 1) {
		$sql = "select (2 * ASIN(SQRT(POW(SIN(($latitude*PI() / 180 - mi.latitude * PI() / 180) / 2),2) + COS(" . $latitude . "*PI() / 180) * COS(mi.latitude * PI() / 180) " . " * POW(SIN((" . $longitude . "*PI() / 180 - mi.longitude * PI() / 180)/2),2)) " . " )) as distance,m.member_id,m.nick,m.email,m.login_time,m.msg_time,m.isteacher,mi.section,mi.filter_type,mi.dreamschool,mi.exam_year,mi.exam_month,mi.exam_address,mi.latitude,mi.longitude, mi.gender,mi.exam,mi.apply_year,mi.avater,mi.year,mi.country,mi.city,mi.dream_school,mi.status,mi.type,mi.major from t_member m left join t_member_info mi on mi.member_id = m.member_id and mi.latitude <> '' and mi.longitude <> '' where m.member_id != '$memberId'" . " order by distance limit $start, $pageSize ";
	} else {
		$sql = "select (2 * ASIN(SQRT(POW(SIN(($latitude*PI() / 180 - mi.latitude * PI() / 180) / 2),2) + COS(" . $latitude . "*PI() / 180) * COS(mi.latitude * PI() / 180) " . " * POW(SIN((" . $longitude . "*PI() / 180 - mi.longitude * PI() / 180)/2),2)) " . " )) as distance,m.member_id,m.nick,m.email,m.login_time,m.msg_time,m.isteacher,mi.section,mi.filter_type,mi.dreamschool,mi.exam_year,mi.exam_month,mi.exam_address,mi.latitude,mi.longitude, mi.gender,mi.exam,mi.apply_year,mi.avater,mi.year,mi.country,mi.city,mi.dream_school,mi.status,mi.type,mi.major from t_member m left join t_member_info mi on mi.member_id = m.member_id and mi.latitude <> '' and mi.longitude <> '' where m.member_id != '$memberId' and mi.exam=$exam" . " order by distance limit $start, $pageSize ";
	}
	
	$rs = $db->Query ( $sql );
	$nSize = count ( $rs );
	$data = array ();
	for($i = 0; $i < $nSize; $i ++) {
		$val = $rs [$i];
		
		$userId = $val ['member_id'];
		$nick = $val ['nick'];
		$email = $val ['email'];
		$latitude = $val ['latitude'];
		$longitude = $val ['longitude'];
		
		$distance = $val ['distance'];
		$distance = $distance * 6378.173 * 2;
		$gender = $val ['gender'];
		if (empty ( $gender )) {
			$gender = '';
		}
		$exam = $val ['exam'];
		if (! isset ( $exam )) {
			$exam = '-1';
		}
		$applyYear = $val ['apply_year'];
		if (empty ( $applyYear )) {
			$applyYear = '';
		}
		$avater = $val ['avater'];
		if (empty ( $avater )) {
			$avater = '';
		}
		$lastLogin = $val ['login_time'];
		if (empty ( $lastLogin )) {
			$lastLogin = '';
		}
		$lastMsg = $val ['msg_time'];
		if (empty ( $lastMsg )) {
			$lastMsg = '';
		}
		
		$year = $val ['year'];
		if (empty ( $year )) {
			$year = '';
		}
		
		$country = $val ['country'];
		if (empty ( $country )) {
			$country = '';
		}
		$city = $val ['city'];
		if (empty ( $city )) {
			$city = '';
		}
		
		$schoolEn = '';
		$schoolCn = '';
		$dreamSchool = $val ['dream_school'];
		if (empty ( $dreamSchool )) {
			$dreamSchool = '';
			$schoolEn = '';
			$schoolCn = '';
		} else {
			$schoolRs = $db->Query ( "select school_id,school_en,school_cn from t_school where school_id=$dreamSchool limit 1" );
			if (empty ( $schoolRs )) {
				$schoolEn = '';
				$schoolCn = '';
			} else {
				$schoolEn = $schoolRs [0] ['school_en'];
				$schoolCn = $schoolRs [0] ['school_cn'];
			}
		}
		
		$status = $val ['status'];
		if (empty ( $status )) {
			$status = '';
		}
		$type = $val ['type'];
		if (! isset ( $type )) {
			$type = 0;
		}
		
		$isTeacher = $val ['isteacher'];
		if (! isset ( $isTeacher )) {
			$isTeacher = '0';
		}
		
		$section = $val ['section'];
		if (empty ( $section )) {
			$section = '';
		}
		
		$filterType = $val ['filter_type'];
		if (! isset ( $filterType )) {
			$filterType = 0;
		}
		
		$dreamSchoolName = $val ['dreamschool'];
		if (empty ( $dreamSchoolName )) {
			$dreamSchoolName = '';
		}
		
		$examYear = $val ['exam_year'];
		if (empty ( $examYear )) {
			$examYear = '';
		}
		
		$examMonth = $val ['exam_month'];
		if (empty ( $examMonth )) {
			$examMonth = '';
		}
		
		$examAddress = $val ['exam_address'];
		if (empty ( $examAddress )) {
			$examAddress = '';
		}
		
		$major = $val ['major'];
		if (empty ( $major )) {
			$major = '';
		}
		
		$data [] = array (
				'email' => $email,
				'nick' => $nick,
				'latitude' => $latitude,
				'longitude' => $longitude,
				'gender' => $gender,
				'exam' => $exam,
				'applyYear' => $applyYear,
				'avater' => $avater,
				'lastLogin' => $lastLogin,
				'lastMsg' => $lastMsg,
				'year' => $year,
				'country' => $country,
				'city' => $city,
				'dreamSchool' => $dreamSchool,
				'dreamSchoolEn' => $schoolEn,
				'dreamSchoolCn' => $schoolCn,
				'status' => $status,
				'distance' => $distance,
				'type' => $type,
				'id' => $userId,
				'isteacher' => $isTeacher,
				'section' => $section,
				'filterType' => $filterType,
				'dreamschoolName' => $dreamSchoolName,
				'examYear' => $examYear,
				'examMonth' => $examMonth,
				'examAddress' => $examAddress,
				'major' => $major 
		);
	}
	
	$dataStr = json_encode ( $data );
	
	echo '{"code":"0","msg":"","pageNo":"' . $pageNo . '","pageSize":"' . $pageSize . '","total":"' . $total . '","data":' . $dataStr . '}';
}

?>